//
//  OriginalViewController.m
//  Drawboard
//
//  Created by A-01 on 2020/6/29.
//  Copyright © 2020 Mac. All rights reserved.
//

#import "OriginalViewController.h"
#import "ChoosePictureViewController.h"
#import "DrawTouchPointView.h"

@interface OriginalViewController ()

@property (nonatomic, weak)UIButton * saveButton;
@property (nonatomic, weak)UIButton * pictureButton;
@property (nonatomic, weak)UIView * toolView;
@property (nonatomic, strong)UIButton * eraserButton;
@property (nonatomic, strong)UIButton * onePenButton;
@property (nonatomic, strong)UIButton * twoPenButton;
@property (nonatomic, strong)UIButton * threePenButton;
@property (nonatomic, strong)UIButton * fourPenButton;
@property (nonatomic, strong)UIImageView * eraserImageView;
@property (nonatomic, strong)UIImageView * onePenImageView;
@property (nonatomic, strong)UIImageView * twoPenImageView;
@property (nonatomic, strong)UIImageView * threePenImageView;
@property (nonatomic, strong)UIImageView * fourPenImageView;
@property (nonatomic, weak)UIView * colorView;
@property (nonatomic, strong)NSArray * colorArray;
@property (nonatomic, strong)UIView * backView;
@property (nonatomic, strong)DrawTouchPointView * drawView;
@property (nonatomic, strong)UIImageView * drawImageView;
@property (nonatomic, weak)UIToolbar *toolbar;
@property (nonatomic, weak)UIView * whiteView;
@property (nonatomic, strong)UIImage * image;

@end

@implementation OriginalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
}

- (void)setupUI{
    
    UIButton * saveButton = [UIButton new];
    
    [saveButton setImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(clickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.saveButton = saveButton;
    
    [self.view addSubview:self.saveButton];
    
    
    UIButton * pictureButton = [UIButton new];
    
    [pictureButton setImage:[UIImage imageNamed:self.img] forState:UIControlStateNormal];
    [pictureButton addTarget:self action:@selector(clickPictureButton) forControlEvents:UIControlEventTouchUpInside];
    pictureButton.backgroundColor = ZRWhiteColor;
    pictureButton.layer.cornerRadius = 10;
    pictureButton.layer.borderWidth = 2;
    pictureButton.layer.borderColor = [ZRBlackColor CGColor];
    pictureButton.layer.masksToBounds = YES;
    
    self.pictureButton = pictureButton;
    
    [self.view addSubview:self.pictureButton];
    
    
    UIView * toolView = [UIView new];
    
    toolView.backgroundColor = ZRWhiteColor;
    toolView.userInteractionEnabled = YES;
    toolView.layer.cornerRadius = 10;
    toolView.layer.borderWidth = 2;
    toolView.layer.borderColor = [ZRBlackColor CGColor];
    toolView.layer.masksToBounds = YES;
    
    self.toolView = toolView;
    
    [self.view addSubview:self.toolView];
    
    
    self.eraserButton = [self setupButtonWithImg:@"eraser"];
    [self.toolView addSubview:self.eraserButton];
    self.onePenButton = [self setupButtonWithImg:@"pen_one"];
    [self.toolView addSubview:self.onePenButton];
    self.twoPenButton = [self setupButtonWithImg:@"pen_two"];
    [self.toolView addSubview:self.twoPenButton];
    self.threePenButton = [self setupButtonWithImg:@"pen_three"];
    [self.toolView addSubview:self.threePenButton];
    self.fourPenButton = [self setupButtonWithImg:@"pen_four"];
    [self.toolView addSubview:self.fourPenButton];
    
    self.eraserImageView = [self setupCheckImageView];
    [self.toolView addSubview:self.eraserImageView];
    self.onePenImageView = [self setupCheckImageView];
    self.onePenImageView.hidden = NO;
    [self.toolView addSubview:self.onePenImageView];
    self.twoPenImageView = [self setupCheckImageView];
    [self.toolView addSubview:self.twoPenImageView];
    self.threePenImageView = [self setupCheckImageView];
    [self.toolView addSubview:self.threePenImageView];
    self.fourPenImageView = [self setupCheckImageView];
    [self.toolView addSubview:self.fourPenImageView];
    
    
    UIView * colorView = [UIView new];
    
    colorView.backgroundColor = ZRWhiteColor;
    colorView.layer.cornerRadius = 20;
    colorView.layer.borderColor = [ZRBlackColor CGColor];
    colorView.layer.borderWidth = 1.5;
    
    self.colorView = colorView;
    
    [self.view addSubview:self.colorView];
    
    [self setupColorButton];
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 20;
    backView.layer.borderColor = [ZRBlackColor CGColor];
    backView.layer.borderWidth = 2;
    backView.userInteractionEnabled = YES;
    backView.layer.masksToBounds = YES;
     
    self.backView = backView;

    [self.view addSubview:self.backView];
    
    self.drawView = [DrawTouchPointView new];
    
    self.drawView.backgroundColor = ZRWhiteColor;
    [self.drawView setStrokeColor:[UIColor colorWithHexString:self.colorArray[0]]];
    [self.drawView setStrokeWidth:2];
    
    [self.backView addSubview:self.drawView];
    
    
    UIImageView * drawImageView = [UIImageView new];
    
    drawImageView.image = [UIImage imageNamed:self.img];
    drawImageView.alpha = 0.3;
    self.drawImageView = drawImageView;
    
    [self.view addSubview:self.drawImageView];
}


- (void)clickAlertSaveButton{
    
    NSArray * arr = DEF_GET_OBJECT(@"image");
    NSMutableArray * imageArr = [NSMutableArray arrayWithArray:arr];
    NSData *imageData = UIImageJPEGRepresentation(self.image,1.0f);
    [imageArr addObject:imageData];
    DEF_SET_OBJECT(imageArr, @"image");
    
    [self clickCloseButton];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)clickAlertShareButton{
    
    NSString *testToShare = @"My Paintings";
    UIImage *imageToShare = self.image;
    NSArray *activityItems = @[testToShare,imageToShare];
     UIActivityViewController *activityVc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityVc animated:YES completion:nil];
}


- (void)clickSaveButton{
    
    //1.开启一个位图上下文
    UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, NO, 1);
    //2.把画板上的内容渲染到上下文当中
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    [self.drawView.layer renderInContext:ctx];
    //3.从上下文当中取出一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4.关闭上下文
    UIGraphicsEndImageContext();

    self.image = newImage;
    
    [self setupToolBarView];
}


- (void)clickPictureButton{
    
    ChoosePictureViewController * chooseVC = [ChoosePictureViewController new];
    
    __weak typeof(self)weakself = self;
    chooseVC.updatePictureBlock = ^(NSString * _Nonnull img) {
        
        [weakself.pictureButton setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    };
    
    [self.navigationController pushViewController:chooseVC animated:YES];
}


- (void)clickBackButton{
    
    if (self.drawView.stroks.count == 0) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                  message:@"Do you want to save the picture？"
                                                                           preferredStyle:UIAlertControllerStyleAlert];
                   
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault
        handler:^(UIAlertAction * action) {
                                                                             
           //1.开启一个位图上下文
           UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, NO, 1);
           //2.把画板上的内容渲染到上下文当中
           CGContextRef ctx =  UIGraphicsGetCurrentContext();
           [self.drawView.layer renderInContext:ctx];
           //3.从上下文当中取出一张图片
           UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
           //4.关闭上下文
           UIGraphicsEndImageContext();

           self.image = newImage;
           
           [self setupToolBarView];
        }];
        
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
        handler:^(UIAlertAction * action) {
            
             [self.navigationController popViewControllerAnimated:YES];
        }];
                   
        [alert addAction:defaultAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
   
}


- (void)clickToolButton:(UIButton *)button{
    
    if (button == self.eraserButton) {
        
        self.eraserImageView.hidden = NO;
        self.onePenImageView.hidden = YES;
        self.twoPenImageView.hidden = YES;
        self.threePenImageView.hidden = YES;
        self.fourPenImageView.hidden = YES;
        
        [self.drawView eraseSreen];
    }else if (button == self.onePenButton) {
        
        self.eraserImageView.hidden = YES;
        self.onePenImageView.hidden = NO;
        self.twoPenImageView.hidden = YES;
        self.threePenImageView.hidden = YES;
        self.fourPenImageView.hidden = YES;
        
        [self.drawView setStrokeWidth:2];
    }else if (button == self.twoPenButton) {
        
        self.eraserImageView.hidden = YES;
        self.onePenImageView.hidden = YES;
        self.twoPenImageView.hidden = NO;
        self.threePenImageView.hidden = YES;
        self.fourPenImageView.hidden = YES;
        
        [self.drawView setStrokeWidth:5];
    }else if (button == self.threePenButton) {
        
        self.eraserImageView.hidden = YES;
        self.onePenImageView.hidden = YES;
        self.twoPenImageView.hidden = YES;
        self.threePenImageView.hidden = NO;
        self.fourPenImageView.hidden = YES;
        
        [self.drawView setStrokeWidth:7];
    }else if (button == self.fourPenButton) {
        
        self.eraserImageView.hidden = YES;
        self.onePenImageView.hidden = YES;
        self.twoPenImageView.hidden = YES;
        self.threePenImageView.hidden = YES;
        self.fourPenImageView.hidden = NO;
        
        [self.drawView setStrokeWidth:10];
    }
}


- (void)mapColorClick:(UIButton *)sender{
    
    for (UIButton * btn in self.colorView.subviews) {
        
        if (btn == sender) {
            
            if (!btn.selected) {
                
                btn.selected = YES;
            }
        }else{
            
            btn.selected = NO;
        }
    }
    [self.drawView setStrokeColor:[UIColor colorWithHexString:self.colorArray[sender.tag]]];
}


-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backButton);
        make.leading.mas_equalTo(self.backButton.mas_trailing).offset(60);
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(36);
    }];
    
    [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.pictureButton);
        make.bottom.mas_equalTo(self.view).offset(-20);
        make.width.mas_equalTo(180);
        make.height.mas_equalTo(100);
    }];
    
    [self.pictureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backButton);
        make.top.mas_equalTo(self.backButton.mas_bottom).offset(13);
        make.bottom.mas_equalTo(self.toolView.mas_top).offset(-10);
        make.width.mas_equalTo(180);
    }];

    [self.eraserButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.toolView).offset(13);
        make.top.mas_equalTo(self.toolView).offset(27);
        make.width.mas_equalTo(32);
        make.height.mas_equalTo(40);
    }];
    
    [self.eraserImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.eraserButton);
        make.top.mas_equalTo(self.eraserButton.mas_bottom);
        make.width.height.mas_equalTo(29);
    }];
    
    [self.onePenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.eraserButton.mas_trailing);
        make.centerY.width.height.mas_equalTo(self.eraserButton);
    }];
    
    [self.onePenImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.onePenButton);
        make.centerY.width.height.mas_equalTo(self.eraserImageView);
    }];
    
    [self.twoPenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.onePenButton.mas_trailing);
        make.centerY.width.height.mas_equalTo(self.onePenButton);
    }];
    
    [self.twoPenImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.twoPenButton);
        make.centerY.width.height.mas_equalTo(self.eraserImageView);
    }];
    
    [self.threePenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.twoPenButton.mas_trailing);
        make.centerY.width.height.mas_equalTo(self.onePenButton);
    }];
    
    [self.threePenImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.threePenButton);
        make.centerY.width.height.mas_equalTo(self.eraserImageView);
    }];
    
    [self.fourPenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.threePenButton.mas_trailing);
        make.centerY.width.height.mas_equalTo(self.onePenButton);
    }];
    
    [self.fourPenImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.fourPenButton);
        make.centerY.width.height.mas_equalTo(self.eraserImageView);
    }];
    
    [self.colorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.toolView.mas_trailing).offset(13);
        make.top.mas_equalTo(self.backButton.mas_bottom).offset(10);
        make.bottom.mas_equalTo(self.view).offset(-20);
        make.width.mas_equalTo(55);
    }];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.colorView.mas_trailing).offset(10);
        make.top.mas_equalTo(self.saveButton);
        make.bottom.mas_equalTo(self.colorView);
        make.trailing.mas_equalTo(self.view).offset(-50);
    }];
    
    [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.mas_equalTo(self.backView).offset(10);
        make.trailing.bottom.mas_equalTo(self.backView).offset(-10);
    }];
    
    [self.drawImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.drawView);
    }];
    
    self.drawImageView.width = self.drawImageView.width * 1.5;
    self.drawImageView.height = self.drawImageView.height * 1.5;
}


-(void)setupColorButton
{
    float viewHeight = kScreenH - 86;
    float Start_X = 19.0f;   // 第一个按钮的X坐标
    float Start_Y = 10.0f;    // 第一个按钮的Y坐标
    float Height_Space = (viewHeight - 224) / 11;     // 竖间距
    float Button_Height = 17.0f;    // 高
    for (int i = 0 ; i < 12; i++) {
        // 圆角按钮
        UIButton *mapBtn = [UIButton new];
        mapBtn.tag = i;//这句话不写等于废了
        mapBtn.frame = CGRectMake(Start_X, i  * (Button_Height + Height_Space)+Start_Y, Button_Height, Button_Height);
        mapBtn.layer.cornerRadius = 17/2;
        mapBtn.layer.masksToBounds = YES;
        mapBtn.backgroundColor = [UIColor colorWithHexString:self.colorArray[i]];
        [mapBtn setImage:[UIImage imageNamed:@"gou"] forState:UIControlStateSelected];
        if (i == 0) {
            mapBtn.selected = YES;
        }
        [self.colorView addSubview:mapBtn];
      //按钮点击方法
        [mapBtn addTarget:self action:@selector(mapColorClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}


- (void)setupToolBarView{
    
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //样式
    toolbar.barStyle = UIBarStyleBlack;//半透明
    //透明度
    toolbar.backgroundColor = ZRBlackColor;
    toolbar.alpha = 0.6f;

    self.toolbar = toolbar;
    [self.navigationController.view addSubview:self.toolbar];
    
    UIView * whiteView = [[UIView alloc] init];
    
    whiteView.backgroundColor = ZRWhiteColor;
    whiteView.userInteractionEnabled = YES;
    whiteView.layer.cornerRadius = 20;
    whiteView.layer.borderWidth = 2;
    whiteView.layer.borderColor = [ZRBlackColor CGColor];
    whiteView.layer.masksToBounds = YES;
    
    self.whiteView = whiteView;
    
    [self.navigationController.view addSubview:self.whiteView];

    
    UIButton * closeButton = [UIButton new];
    
    [closeButton setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(clickCloseButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.whiteView addSubview:closeButton];
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.backgroundColor = ZRWhiteColor;
    imageView.image = self.image;
    
    [self.whiteView addSubview:imageView];
    
    
    UIButton * saveButton = [UIButton new];
    
    [saveButton setTitle:@"save" forState:UIControlStateNormal];
    [saveButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(clickAlertSaveButton) forControlEvents:UIControlEventTouchUpInside];
    [saveButton setBackgroundColor:[UIColor colorWithHexString:@"EDFF1C"]];
    saveButton.layer.cornerRadius = 15;
    saveButton.layer.borderWidth = 2;
    saveButton.layer.borderColor = [ZRBlackColor CGColor];
    
    [self.whiteView addSubview:saveButton];
    
    
    UIButton * shareButton = [UIButton new];
    
    [shareButton setTitle:@"share" forState:UIControlStateNormal];
    [shareButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(clickAlertShareButton) forControlEvents:UIControlEventTouchUpInside];
    [shareButton setBackgroundColor:[UIColor colorWithHexString:@"EDFF1C"]];
    shareButton.layer.cornerRadius = 15;
    shareButton.layer.borderWidth = 2;
    shareButton.layer.borderColor = [ZRBlackColor CGColor];
    
    [self.whiteView addSubview:shareButton];
    
    
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
        make.width.mas_equalTo(510);
        make.height.mas_equalTo(290);
    }];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.whiteView).offset(-15);
        make.top.mas_equalTo(self.whiteView).offset(15);
        make.width.height.mas_equalTo(44);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.whiteView);
        make.top.mas_equalTo(self.whiteView).offset(45);
        make.height.mas_equalTo(150);
        make.width.mas_equalTo(250);
    }];
    
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.whiteView).offset(120);
        make.bottom.mas_equalTo(self.whiteView).offset(-20);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(45);
    }];

    [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.whiteView).offset(-120);
        make.centerY.width.height.mas_equalTo(saveButton);
    }];
}


- (void)clickCloseButton{
    
    [self.toolbar removeFromSuperview];
    [self.whiteView removeFromSuperview];
}

- (UIButton *)setupButtonWithImg:(NSString *)img{
    
    UIButton * button = [UIButton new];
    
    [button setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickToolButton:) forControlEvents:UIControlEventTouchUpInside];

    return button;
}


- (UIImageView *)setupCheckImageView{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:@"check"];
    imageView.hidden = YES;

    return imageView;
}


- (NSArray *)colorArray{
    
    if (!_colorArray) {
        
        _colorArray = @[@"E60012",@"E65601",@"FFF100",@"65EB5C",@"0AA000",@"ED73CB",@"A50FB0",@"1D22BA",@"39A0FC",@"9D7264",@"58393A",@"000000"];
    }
    
    return _colorArray;
}


@end
